package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.DamageListGoodsDao;
import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.UserDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.DamageListGoodsService;
import com.atguigu.jxc.service.LogService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author :ttm
 * @description :
 */
@Service
public class DamageListGoodsServiceImpl implements DamageListGoodsService {
    @Autowired
    private DamageListGoodsDao damageListGoodsDao;
    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private LogService logService;
    @Autowired
    private UserDao userDao;

    @Override
    public ServiceVO save(DamageList damageList, String damageListGoodsStr) {
        Gson gson = new Gson();
        List<DamageListGoods> damageListGoodsList = gson.fromJson(damageListGoodsStr, new TypeToken<List<DamageListGoods>>() {
        }.getType());
        //获取用户id
        User currentUser = userDao.findUserByName((String) SecurityUtils.getSubject().getPrincipal());
        Integer userId = currentUser.getUserId();
        damageList.setUserId(userId);
        //保存报损商品
        damageListGoodsDao.saveDamageList(damageList);
        for (DamageListGoods damageListGoods : damageListGoodsList) {
            damageListGoods.setDamageListId(damageList.getDamageListId());
            damageListGoodsDao.saveDamageListGoods(damageListGoods);
            Goods goods = goodsDao.findByGoodsId(damageListGoods.getGoodsId());
            goods.setInventoryQuantity(goods.getInventoryQuantity() - damageListGoods.getGoodsNum());
            goods.setState(2);
            goodsDao.updateGoods(goods);
        }
        logService.save(new Log(Log.INSERT_ACTION, "新增商品报损单：" + damageList.getDamageNumber()));

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public Map<String, Object> list(String sTime, String eTime) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            List<DamageList> damageList = damageListGoodsDao.getDamagelist(sTime, eTime);
            map.put("rows", damageList);
            logService.save(new Log(Log.SELECT_ACTION, "商品报损单据查询"));

        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    @Override
    public Map<String, Object> goodsList(Integer damageListId) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            List<DamageListGoods> damageListGoodsList=damageListGoodsDao.getDamageListGoodsByDamageListId(damageListId);
            map.put("rows",damageListGoodsList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
}
